package com.liuxin.mycontactdemo.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.liuxin.mycontactdemo.dbhelper.ContactOpenHelper;

public class ContactProvider extends ContentProvider {
    private  static final String AUTHORITIES=ContactProvider.class.getCanonicalName();
    private ContactOpenHelper openHelper;
    static UriMatcher uriMatcher;
    public static final Uri URI_CONTACT=Uri.parse("content://"+AUTHORITIES+"/contact");
    private  static  final  int CONTACT=1;
    static
    {
        uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITIES,"/contact",CONTACT);
    }
    @Override
    public boolean onCreate() {
        openHelper=new ContactOpenHelper(getContext());
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] columns, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        int match=uriMatcher.match(uri);
        Cursor cursor=null;
        switch (match)
        {
            case  CONTACT:
                SQLiteDatabase db=openHelper.getReadableDatabase();
                cursor=db.query(ContactOpenHelper.T_CONTACT,columns,selection,selectionArgs,null,null,sortOrder);
                Log.i("ContactProvider","ContactProvider query success:");
             default:break;
        }
        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues cv) {
        int match=uriMatcher.match(uri);
        Cursor cursor=null;
        switch (match)
        {
            case  CONTACT:
                SQLiteDatabase db=openHelper.getWritableDatabase();
                long id=db.insert(ContactOpenHelper.T_CONTACT,"",cv);
                if(id!=-1)
                {
                    Log.i("ContactProvider","ContactProvider insert success:");
                    //拼接生成新的uri
                    uri= ContentUris.withAppendedId(uri,id);
                    getContext().getContentResolver().notifyChange(ContactProvider.URI_CONTACT,null);
                }
                break;
            default:break;
        }
        return uri;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String whereClause, @Nullable String[] whereArgs) {
        int match=uriMatcher.match(uri);
        int deletecount=0;
        switch (match)
        {
            case  CONTACT:
                SQLiteDatabase db=openHelper.getWritableDatabase();
                deletecount=db.delete(ContactOpenHelper.T_CONTACT,whereClause,whereArgs);
                if(deletecount>0)
                {
                    Log.i("ContactProvider","ContactProvider delete success:");
                    getContext().getContentResolver().notifyChange(ContactProvider.URI_CONTACT,null);//发送通知
                }
                break;
            default:break;
        }
        return deletecount;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues cv, @Nullable String whereClause, @Nullable String[] whereArgs) {
        int match=uriMatcher.match(uri);
        int updatecount=0;
        switch (match)
        {
            case  CONTACT:
                SQLiteDatabase db=openHelper.getWritableDatabase();
                updatecount=db.update(ContactOpenHelper.T_CONTACT,cv,whereClause,whereArgs);
                if(updatecount>0)
                {
                    Log.i("ContactProvider","ContactProvider update success:");
                }
                break;
            default:break;
        }
        return updatecount;
    }
}
